//
//  TabbarView.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/16.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

class TabbarView: UITabBar {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height * 0.5)
        
        
        
        // 每一个按钮的宽度
        let itemW = self.frame.width / 5
        // 当前遍历到的 UITabBarButton 的位置
        var index = 0
        for value in subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                // 设置宽度
                value.frame.size.width = itemW
                // 设置x值
                value.frame.origin.x = CGFloat(index) * itemW
                index += 1
                // 当判断到 index 为 2 --> 发现的按钮，再往后移一个位置
                if index == 2 {
                    index += 1
                }
            }
        }
    }
    
    
    
    
    private lazy var composeButton:UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        
        
        
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        button.sizeToFit()
        
        return button
        
    }()
    
//    func composeButtonClick() {
//        
//    }
    
    var composeButtonClickClosure:(() -> ())?
    
    func composeButtonClick(){
        composeButtonClickClosure?()
    }
    

}
